package com.heima.homework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

public class Demo02 {

 /*   public static void main(String[] args) throws Exception {
        // 1.从文件读取数据
        FileReader fr = new FileReader("hero.txt", Charset.forName("GBK"));
        BufferedReader br = new BufferedReader(fr);
        String head = br.readLine();// 表头
        String content;



        // 创建集合用于存储 英雄对象
        //ArrayList<Hero> heroes = new ArrayList<>();
        TreeSet<Hero> heroes = new TreeSet<>(new Comparator<Hero>() {
            @Override
            public int compare(Hero o1, Hero o2) {
                // 设置两个排序字段  第一排序 分值 第二排序 姓名
                int res = Double.compare(o2.getScore(),o1.getScore());
                if(res == 0){
                    return o1.getName().compareTo(o2.getName());
                }else {
                    return res;
                }
            }
        });
        while ((content = br.readLine())!= null){
        // 2.读取的数据封装成对象
            String[] split = content.split(",+");
            Hero hero = new Hero(split[0], Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3]));
            heroes.add(hero);
        }

        br.close();

        System.out.println(heroes);

        // 4.写回到文件
        FileWriter fw = new FileWriter("hero.txt", Charset.forName("GBK"));
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(head+" 新手适应度分值");
        bw.newLine();

        for (Hero hero : heroes) {
            bw.write(hero.getName()+","+hero.getHealth()+","+hero.getAttack()+","+hero.getEffect()+","+hero.getScore());
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }*/
}
